﻿using AdminSystem.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Repositories.lmplements
{
    /// <summary>
    /// 仓储基类
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    public class BaseRepositories<T> : IBaseRepositories<T> where T : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly BaseDBContext db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db">上下文</param>
        public BaseRepositories(BaseDBContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<T> Add(T entity)
        {
          await db.Set<T>().AddAsync(entity);
            await db.SaveChangesAsync();
            return entity;
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">实体</param>
        /// <returns></returns>
        public async Task<int> AddRange(IEnumerable<T> entities)
        {
            db.Set<T>().AddRange(entities);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public async Task<int> Delete(int id)
        {
            db.Set<T>().Remove(db.Set<T>().Find(id));
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            db.Set<T>().RemoveRange(db.Set<T>().Find(ids));
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 查询条件是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public async Task<bool> ExistsAsync(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().AnyAsync();
        }
        /// <summary>
        /// 根据条件查询多条
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public async Task<List<T>> GetAll(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().Where(expression).ToListAsync();
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public async Task<IList<T>> GetAll()
        {
            return await db.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetById(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="expression"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<T>> QueryAsync<TKey>(Expression<Func<T, bool>> expression, Expression<Func<T, TKey>> orderby)
        {
             return await db.Set<T>().Where(expression).OrderBy(orderby).ToListAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Update(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> UpdateRange(IEnumerable<T> entities)
        {
            db.Set<T>().UpdateRange(entities);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回是影响的行数</returns>

        public async Task<int> DeleteAsync(T entity)
        {
            db.Set<T>().Remove(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件表达式获取单个实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<T> GetByConditionAsync(Expression<Func<T, bool>> condition)
        {
            return await db.Set<T>().FirstOrDefaultAsync(condition);
        }
    }
}
